'''
Created on Nov 6, 2010

@author: jtk
'''

import socket
testing = socket.gethostname() != 'pc.cs.purdue.edu'

from os.path import expanduser
from os import stat

def script(userid, player):
    if testing:
        return 'applications/icypc/private/tests/%s-%s' % (userid, player)  # should be request.application, but request is not known here
    return expanduser('~' + str(userid)) + '/icypc/' + str(player)

def scriptRunnable(logger, userid, player):
    s = script(userid, player)
    if s.startswith('~'):
        logger.error('user id "%s" did not expand' % userid)
        return False
    try:
        mode = stat(s).st_mode  # get mode bits of script file
    except OSError:
        logger.error("script %s not found" % s)
        return False
    if (mode & 5) != 5: # check for +rx bit set for world
        logger.error('script %s is not executable (r+x) for world' % s)
        return False
    return True
